/**
 * Created by jiachenpan on 16/11/18.
 */

export function formatTime(time, format) {
  var t = new Date(time * 1000)
  var tf = function(i) {
    return (i < 10 ? '0' : '') + i
  }
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear())
      case 'MM':
        return tf(t.getMonth() + 1)
      case 'mm':
        return tf(t.getMinutes())
      case 'dd':
        return tf(t.getDate())
      case 'HH':
        return tf(t.getHours())
      case 'ss':
        return tf(t.getSeconds())
    }
  })
}

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

// 根据文件名后缀区分 文件类型
export function fileTypeMatch(fileName) {
  var str = fileName.substring(fileName.lastIndexOf('.'))
  if (str === '.png' || str === '.jpg' || str === '.jpeg' || str === '.bmp' || str === '.gif') {
    return 'img'
  }
  if (str === '.mp4' || str === '.rmvb' || str === '.avi' || str === '.ogg') {
    return 'video'
  }
}
